<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>注册页面</h1>
<form action="">
    <!--@blur添加失去焦点事件-->
    <input type="text" name="username" placeholder="用户名"
           @blur="check()">{{info}}<br>
    <input type="text" name="password" placeholder="密码">
    <input type="button" value="注册" @click="reg()">
</form>
<!--引入vue和axios-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.js"></script>
<script>
    let v = new Vue({
        el:"form",
        data:{
            info:""
        },
        methods:{
            reg:function () {
                //通过form表单创建一个表单数据对象
                //这种写法会自动将表单中的参数装进 data里面
                let data = new FormData(document.querySelector("form"));

                axios.post("/reg",data).then(function (response) {
                    alert(response.data);
                })
            },
            check:function () {
                //发出异步请求
                let data = new FormData(document.querySelector("form"));
                axios.post("/check",data).then(function (response) {
                    if (response.data==1){
                        v.info="用户名可用!";
                    }else{
                        v.info="用户名已存在!";
                    }
                });
            }
        }
    })
</script>
</body>
</html>